﻿Imports System.IO
Imports System.Text.RegularExpressions
Public Class 解释器
    Private index As New Dictionary(Of String, String)
    Public Function Search(key As String) As String
        If key Is Nothing Then
            Return ""
        End If
        If index.ContainsKey(key) Then
            Return String.Format(" ({0})", index(key))
        End If
        Return ""
    End Function
    Public Sub New(rootPath As String)
        If Not Directory.Exists(rootPath) Then
            Return
        End If
        Dim files = Directory.GetFiles(rootPath)
        For Each f In files
            If Path.GetExtension(f) = ".txt" Then
                LoadIndex(File.ReadAllText(f))
            End If
        Next
    End Sub
    Private Sub LoadIndex(content As String)
        Dim lines() As String = Split(content, vbCrLf)
        Dim errorResult As New List(Of String)
        For Each line In lines
            If Regex.IsMatch(line, "^.* .*$") Then
                Dim key = Regex.Match(line, "^(.*?) .*$").Groups(1).Value
                Dim value = Regex.Match(line, "^.*? (.*)$").Groups(1).Value
                If index.ContainsKey(key) Then
                    errorResult.Add(String.Format("重复键值{0}！", key))
                Else
                    index.Add(key, value)
                End If
            End If
        Next
        If errorResult.Count > 0 Then
            MsgBox(Join(errorResult.ToArray, vbCrLf), 64, "解释器.LoadIndex")
        End If
    End Sub
End Class
